
package client;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.Random;
import javax.swing.JPanel;


/**
 *
 * @author Ma C
 */
public class NewJPanel extends javax.swing.JPanel {
   
    private BeenTree Binary_tree = new BeenTree();
   
       
   
    /** Creates new form NewJPanel */
    public NewJPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents

        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        num_random = new javax.swing.JComboBox();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        setBackground(new java.awt.Color(119, 142, 164));
        setBorder(new javax.swing.border.SoftBevelBorder(0));
        setToolTipText("");

        jButton1.setText("Add Value");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Search Value");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("DeleteValue");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel1.setText("Binary Tree");

        num_random.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "10", "100", "1000", "10000", "100000", "1000000" }));
        num_random.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                num_randomActionPerformed(evt);
            }
        });

        jButton4.setText("Generate Random");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Generate File");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(num_random, 0, 67, Short.MAX_VALUE)
                            .addComponent(jTextField1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton4)
                                .addGap(18, 18, 18)
                                .addComponent(jButton5))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addGap(18, 18, 18)
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton3))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(174, 174, 174)
                        .addComponent(jLabel1)))
                .addContainerGap(57, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton2)
                        .addComponent(jButton3)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(num_random, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4)
                    .addComponent(jButton5))
                .addContainerGap(203, Short.MAX_VALUE))
        );
    }//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    String text =jTextField1.getText();
        jTextField1.setText("");
        Binary_tree.addValue(Integer.parseInt(text));
        //this.repaint();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    String text =jTextField1.getText();
        jTextField1.setText("");
    
        Node result = Binary_tree.Search(Integer.parseInt(text));
        if (result == null)
            JOptionPane.showMessageDialog(null, "The value wasn't found", "info",
                                          JOptionPane.INFORMATION_MESSAGE); // TODO add your handling code here:
        else {
            if (result.getPredecesor() == null) {
                JOptionPane.showMessageDialog(null, "The value is the root", "info",
                                              JOptionPane.INFORMATION_MESSAGE); // TODO add your handling code here:

            }else {
                String message = "The value was found in the level ";
             message = message.concat(Integer.toString(Binary_tree.level));
                message =message.concat(" and its predecesor is: ");
                message = message.concat(Integer.toString(result.getPredecesor().getValue()));
                JOptionPane.showMessageDialog(null, message, "info",
                                              JOptionPane.INFORMATION_MESSAGE); // TODO add your handling code here:
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
String text =jTextField1.getText();
jTextField1.setText("");
        Binary_tree.delete(Integer.parseInt(text));
        //this.repaint();// TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void num_randomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_num_randomActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_num_randomActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        Object item = num_random.getSelectedItem();
        Binary_tree =new BeenTree();
        repaint();
   generateNumbers( Integer.valueOf(item.toString()));
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
Binary_tree.printFile();        // TODO add your handling code here:
    }//GEN-LAST:event_jButton5ActionPerformed
    
    public void paint(Graphics g) {
        super.paint(g);
        String treeValue;
        int x, y;
        x = this.getWidth()/2;
        y = 120;
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Font font = new Font("Serif", Font.PLAIN, 20);
        g2.setFont(font);
        if (!Binary_tree.isEmpty()) {
            g2.drawString(Integer.toString(Binary_tree.getRoot().getValue()), x, y);
            Node comp = Binary_tree.getRoot();
            String R;
            String L;
            
            printBranchL(comp, g2,x, y);
            printBranchR(comp, g2,x,y);
        

            /*  String R;
                 String L;
                 if (Binary_tree.getRoot().getRChild()!=null)
                 { R = Integer.toString(Binary_tree.getRoot().getRChild().getValue());}else R= " ";
                 if (Binary_tree.getRoot().getLChild()!=null)
                 { L = Integer.toString(Binary_tree.getRoot().getRChild().getValue());}else L= " ";
                 if (L== null) L = " ";
                 x= x-40;
                 y=y-20;
                 //g2.drawString(L, x, y);
                 //g2.drawString(R, x+80, y);
                 //comp = comp.getLChild();*/
            //}
            // g2.drawString("1", 1000/3, 120);
        }
        else{
            g2.drawString(" ", x, y);
        }
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.getContentPane().add(new NewJPanel());
        f.setSize(1000, 700);
        f.setVisible(true);
    }

    public void printBranchL(Node comp, Graphics2D g2, int x, int y) {
       
        String R;
        if (comp.getLChild()==null) {
            g2.drawString(" ", x - 20, y);  
        }else{
        while ((comp.getLChild() != null)) {
            if (comp.getLChild() != null) {
                R = Integer.toString(comp.getLChild().getValue());
                y = y + 40;
                g2.drawString(R, x - 90, y);
                // comp=comp.getLChild();

            }

            x = x - 90;
            y = y - 20;
            comp = comp.getLChild();
            printBranchR(comp, g2, x, y);
        }
        }

    }


    public void printBranchR(Node comp, Graphics2D g2, int x, int y) {
        
        String R;
        if (comp.getRChild()==null) {
            g2.drawString(" ", x - 20, y);  
        }else{
        while (comp.getRChild() != null) {
            if (comp.getRChild() != null) {
                R = Integer.toString(comp.getRChild().getValue());
                y = y + 40;
                g2.drawString(R, x + 20 , y);
                // comp=comp.getLChild();

            }

            x = x + 20;
            y = y - 20;
            comp = comp.getRChild();
            printBranchL(comp, g2,  x,  y);
        }
        }
        

    }
    
    public void generateNumbers(int num) {
        int insert= 0;
        Random ram= new Random();
       System.out.println(num);
        for (int i=0; i<num; i++)
        {
            insert=ram.nextInt(100000000);
            System.out.println(insert);
         
            Binary_tree.addValue(insert);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JComboBox num_random;
    // End of variables declaration//GEN-END:variables

}
